# -*- coding: utf-8 -*-

from django.db import models
from core.models import CoreModel, table_prefix
from django.utils.translation import gettext_lazy as _
import reversion


@reversion.register()
class TestCase(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="testcase_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    module = models.ForeignKey(
        to="pms.Module",
        related_name="testcase_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属模块",
    )
    number = models.CharField(unique=True, max_length=256, verbose_name="用例编号")
    name = models.CharField(max_length=1024, verbose_name="用例名称")
    reqs = models.ManyToManyField(
        to="pms.Requirement",
        related_name="testcase_set",
        db_constraint=False,
        verbose_name="关联需求",
    )
    level = models.CharField(max_length=50, default="0", verbose_name="用例级别")
    type = models.CharField(max_length=50, default="0", verbose_name="用例类型")
    stage = models.CharField(max_length=50, default="0", verbose_name="适用阶段")
    isauto = models.CharField(max_length=50, default="Y", verbose_name="是否可自动化")
    description = models.TextField(null=True, blank=True, verbose_name="测试说明")
    precondition = models.TextField(null=True, blank=True, verbose_name="预置条件")
    steps = models.JSONField(default=dict, verbose_name="操作步骤")
    script = models.TextField(null=True, blank=True, verbose_name="用例脚本")

    def __str__(self) -> str:
        return self.number

    class Meta:
        db_table = table_prefix + "tms_testcase"
        verbose_name = "用例管理"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)
